package com.qf.service.impl;

import com.qf.dao.ProductDao;
import com.qf.dao.impl.ProductDaoImpl;
import com.qf.pojo.PageBean;
import com.qf.pojo.Product;
import com.qf.service.ProductService;

import java.sql.SQLException;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    @Override
    public List<Product> findProduct() {
        try {
            ProductDao productDao = new ProductDaoImpl();
            List<Product> list = productDao.selectProduct();
//            System.out.println(list);
            if (list!=null){
                return list;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> hotProduct() {
        try {
            ProductDao productDao = new ProductDaoImpl();
            List<Product> list = productDao.selectHotProduct();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Product findProductByid(String pid) {
        //调用数据访问层
        try {
            ProductDao productDao = new ProductDaoImpl();
            Product product = productDao.selectProductById(pid);
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageBean<Product> findProductByPage(int currentPage, int pageSize, String cid) {
        //调用数据访问层
        try {
            ProductDao productDao = new ProductDaoImpl();
            List<Product> list = productDao.selectProductByPage(currentPage,pageSize,cid);
            //调用业务层方法,查询总记录数
           int totalCount = productDao.findTotalCountByCid(cid);
            return new PageBean<Product>(currentPage,pageSize,totalCount,list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
